<?php
class ModelReportProduct extends Model {
	public function getProductsViewed($filter) {
		$sql = "SELECT pd.name, p.model, p.viewed FROM  @@product p LEFT JOIN  @@product_description pd ON (p.product_id = pd.product_id) WHERE pd.language_id = '" . (int)C('config_language_id') . "' AND p.viewed > 0 ORDER BY p.viewed DESC";

		$sql .= $filter->limit();
		
		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalProductsViewed() {
      	return $this->db->queryOne("SELECT COUNT(*) AS total FROM  @@product WHERE viewed > 0");
	}

	public function getTotalProductViews() {
      	return $this->db->queryOne("SELECT SUM(viewed) AS total FROM  @@product");
	}

	public function reset() {
		$this->db->query("UPDATE  @@product SET viewed = 0");
	}

	public function getPurchased($filter) {
		$sql = "SELECT op.name, op.model, SUM(op.quantity) AS quantity, SUM(op.total + op.total * op.tax / 100) AS total FROM  @@order_product op LEFT JOIN `@@order` o ON (op.order_id = o.order_id)";

		if ($filter->get('filter_order_status_id')) {
			$sql .= " WHERE o.order_status_id = " . (int)$filter->get('filter_order_status_id');
		} else {
			$sql .= " WHERE o.order_status_id > 0";
		}

		if ($filter->get('filter_date_start')) {
			$sql .= " AND DATE(o.date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
		}

		if ($filter->get('filter_date_end')) {
			$sql .= " AND DATE(o.date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
		}

		$sql .= " GROUP BY op.model ORDER BY total DESC";

		$sql .= $filter->limit();

		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalPurchased($filter) {
      	$sql = "SELECT COUNT(DISTINCT op.model) AS total FROM `@@order_product` op LEFT JOIN `@@order` o ON (op.order_id = o.order_id)";
      	
		if ($filter->get('filter_order_status_id')) {
			$sql .= " WHERE o.order_status_id = " . (int)$filter->get('filter_order_status_id');
		} else {
			$sql .= " WHERE o.order_status_id > 0";
		}

		if ($filter->get('filter_date_start')) {
			$sql .= " AND DATE(o.date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
		}

		if ($filter->get('filter_date_end')) {
			$sql .= " AND DATE(o.date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
		}

		return $this->db->queryOne($sql);
	}
}
?>